// 号码下拉框点击显示或点击取消函数
function drop() {
  let $show = $(".mz-input>dl.d-none");
  if ($show.length == 1) {
    $show.removeClass("d-none");
  } else {
    $(".mz-input>dl").addClass("d-none");
  }
}
$("#phoneid").click(drop);

$(".mz-close").click(function(){
  tips()
})

$("dd").click(function () {
  let $this = $(this);
  let text = $this.text()
  let n = text.indexOf("+");
  let num = text.slice(n);
  $("#phoneid>span").html(num);
  // 点击选择后调用下拉框取消函数
  drop();
})

// 密码点击隐藏
$(".mz-pwd-icon").click(function(){
  if(upwd.type === "text"){
    upwd.type="password"
    $(this).css("background-position-x","-66px")
  }else{
    upwd.type="text"
    $(this).css("background-position-x","-35px")
  }
})

$(".mz-btn1").click(btn1);
$(".mz-btn2").click(reg);

function btn1(){
  tips()
  let $phone = uphone.value;
  let reg = /^1[3-9]\d{9}$/;
  if (!$phone) {
    tips("请输入手机号码",true)
  } else if (!reg.test($phone)) {
    tips("手机号码格式错误",true)
  } else if (!server.checked){
    tips("请先同意用户条款",true)
  } else{
    isexists($phone)
  } 
}
function reg() {
  tips()
  let $phone = uphone.value;
  let $upwd = upwd.value;
  let reg = /(?!^(\d+|[a-zA-Z]+|[~!@#$%^&*?]+)$)^[\w~!@#$%^&*?]{7,20}$/;
  if (!$upwd) {
    tips("密码不能为空",true)
  } else if (!reg.test($upwd)) {
    tips("密码需为 8-16 位大小写字母、数字、特殊字符，至少包含两种类型，不得与用户名相同",true)
  } else {
      let xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
          if (xhr.responseText == 1) {
            location.href = "index.html"
          }
        }
      }
      let url = "http://127.0.0.1:8080/mz/register"
      xhr.open("post", url, true)
      xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
      xhr.send(`phone=${$phone}&upwd=${$upwd}`)
  }
}

//判断手机号码是否已经注册过
function isexists(phone) {
  let xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function () {
    if(xhr.readyState === 4 && xhr.status === 200){
      let r = xhr.responseText;
      if (r == 1) {
        tips("手机号码已被注册",true)
        return
      } else{
        tips()
        console.log(1);
        $(".mz-register-2").css("display","block")
      }
    }
  }
  let url = `http://127.0.0.1:8080/mz/isexists?phone=${phone}`;
  xhr.open("get", url, true);
  xhr.send();
}

// tips框显示隐藏
// bool为是否隐藏，true为显示
function tips(text,bool){
  if(bool){
    $(".mz-tips").css("visibility","visible")
    $(".mz-tips-info").html(`${text}`)
  }else{
    $(".mz-tips").css("visibility","hidden")
  }
}